package net.butfly.bus;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import java.io.Serializable;

public class Response implements Serializable {

	private static final long serialVersionUID = 5897857767191140750L;

	/**
	 * 执行结果标志 -2:尚未执行完成;0:成功;-1失败
	 */
	private int resultCode = -2;

	/**
	 * 0:system error, -1: business error
	 */
	private int errorType = 0;

	/**
	 * 具体错误代码
	 */
	private String errorCode;

	/**
	 * 错误信息
	 */
	private String errorMessage;

	/**
	 * 错误堆栈信息
	 */
	private String errorStack;

	// --------------------------------------------------------------------------------
	/**
	 * 调用结果
	 */
	private Object result;

	// --------------------------------------------------------------------------------
	public Object getResult() {
		return result;
	}

	public void setResult(Object result) {
		this.result = result;
	}

	// --------------------------------------------------------------------------------
	public int getResultCode() {
		return resultCode;
	}

	public boolean isFinished() {
		return resultCode != -2;
	}

	public boolean hasError() {
		return resultCode == -1;
	}

	public int getErrorType() {
		return errorType;
	}

	public void setErrorTypeOfBusi() {
		errorType = -1;
	}

	public boolean isErrorOfBusi() {
		return errorType == -1;
	}

	public void setExecuteSuccess() {
		resultCode = 0;
	}

	public void setExecuteFail() {
		resultCode = -1;
	}

	public String getErrorCode() {
		return errorCode;
	}

	public void setErrorCode(String errorCode) {
		this.errorCode = errorCode;
	}

	public String getErrorMessage() {
		return errorMessage;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

	public String getErrorStack() {
		return errorStack;
	}

	public void setErrorStack(String errorStack) {
		this.errorStack = errorStack;
	}

	public void setErrorStack(Exception ex) {
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		ex.printStackTrace(new PrintWriter(os, true));
		this.setErrorStack(new String(os.toByteArray()));
	}

	// --------------------------------------------------------------------------------
	public void copyFrom(Response resp) {
		resultCode = resp.getResultCode();
		errorType = resp.getErrorType();
		errorCode = resp.getErrorCode();
		errorMessage = resp.getErrorMessage();
		errorStack = resp.getErrorStack();
		result = resp.getResult();
	}

}
